package com.example.everying.controller.content;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.everying.annotation.WebLog;
import com.example.everying.context.UserContext;
import com.example.everying.model.dto.content.ActivityRatingDto;
import com.example.everying.model.entity.ActivityRating;
import com.example.everying.model.vo.content.ActivityArticleVo;
import com.example.everying.model.vo.user.UserInfoVo;
import com.example.everying.service.content.ActivityRatingService;
import com.example.everying.utils.PageParam;
import com.example.everying.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

import com.baomidou.mybatisplus.core.metadata.IPage;

/**
 * 活动评价控制器
 */
@RestController
@RequestMapping("/content/rating")
@Api(tags = "活动评价")
public class ActivityRatingController {
    @Resource
    private ActivityRatingService activityRatingService;

    /**
     * 新增评价
     */
    @PostMapping("/save")
    @ApiOperation(value = "新增评价")
    @WebLog(description = "新增评价")
    public Result<?> saveRating(@RequestBody ActivityRating rating) {

        return activityRatingService.saveRating(rating)?Result.ok() : Result.fail();
    }

    /**
     * 更新评价
     */
    @PostMapping("/update")
    public boolean updateRating(@RequestBody ActivityRating rating) {
        return activityRatingService.updateRating(rating);
    }

    /**
     * 删除评价
     */
    @GetMapping("/delete/{id}")
    public boolean deleteRating(@PathVariable String id) {
        return activityRatingService.deleteRating(id);
    }

    /**
     * 获取所有评价列表
     */
    @GetMapping("/list")
    public List<ActivityRating> getAllRatings() {
        return activityRatingService.getAllRatings();
    }

    /**
     * 根据ID获取评价详情
     */
    @GetMapping("/{id}")
    public ActivityRating getRatingById(@PathVariable String id) {
        return activityRatingService.getRatingById(id);
    }

    /**
     * 查询我发布的待评价活动
     */
    @GetMapping("/published/list")
    public Result<?> getPublishedToRate(@RequestBody PageParam<ActivityArticleVo> query) {
        UserInfoVo userInfoVo = UserContext.getUser();
        String currentUserId = userInfoVo.getId();
        Page<ActivityArticleVo> pageParam = new Page<>(query.getCurrent(), query.getLimit());

        IPage<ActivityArticleVo> pageModel = activityRatingService.getPublishedToRate(pageParam, currentUserId);
        return Result.ok(pageModel);
    }

    /**
     * 查询我参与的待评价活动
     */
    @GetMapping("/participated/list")
    public Result<?> getParticipatedToRate(@RequestBody PageParam<ActivityArticleVo> query) {
        UserInfoVo userInfoVo = UserContext.getUser();
        String currentUserId = userInfoVo.getId();
        Page<ActivityArticleVo> pageParam = new Page<>(query.getCurrent(), query.getLimit());
        IPage<ActivityArticleVo> pageModel = activityRatingService.getParticipatedToRate(pageParam, currentUserId);
        return Result.ok(pageModel);
    }

    @PostMapping("/delete")
    public Result<?> deleteViewRating(@RequestBody ActivityRating  rating){
        return activityRatingService.deleteViewRating(rating);
    }
    @PostMapping("/read")
    public Result<?> readViewRating(@RequestBody ActivityRatingDto rating){
        return activityRatingService.readRating(rating);
    }
}
